<template>
  <el-dialog
    :title="!dataForm.seqno ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">

    <el-form-item label="登记中心" prop="orgid">
      <el-select v-model="dataForm.orgid" placeholder="登记中心" style="width:100%">
          <el-option
            v-for="item in dataForm.bankList"
            :key="item.bankid"
            :label="item.bankname"
            :value="item.bankid">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="银行参与者" prop="bankid">
      <el-select v-model="dataForm.bankid" placeholder="银行参与者" style="width:100%">
          <el-option
            v-for="item in dataForm.bankList2"
            :key="item.bankid"
            :label="item.bankname"
            :value="item.bankid">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="登录账号" prop="login">
      <el-input v-model="dataForm.login" placeholder="登录账号"></el-input>
    </el-form-item>

    <el-form-item label="sftp账号" prop="sftp">
      <el-input v-model="dataForm.sftp" placeholder="sftp账号"></el-input>
    </el-form-item>

    <el-row>
        <el-col :span="12">
                <el-form-item label="登录密码" prop="loginpass" :class="{ 'is-required': !dataForm.seqno }">
                  <el-input v-model="dataForm.loginpass" type="password" placeholder="登录密码"></el-input>
                </el-form-item>
        </el-col>
        <el-col :span="12">
                <el-form-item label="确认登录密码" prop="comfirmLoginpass" :class="{ 'is-required': !dataForm.seqno }">
                  <el-input v-model="dataForm.comfirmLoginpass" type="password" placeholder="确认登录密码"></el-input>
                </el-form-item>
        </el-col>
    </el-row>

    <el-row>
        <el-col :span="12">
                <el-form-item label="sftp密码" prop="sftppass" :class="{ 'is-required': !dataForm.seqno }">
                  <el-input v-model="dataForm.sftppass" type="password" placeholder="sftp密码"></el-input>
                </el-form-item>
        </el-col>
        <el-col :span="12">
                <el-form-item label="确认sftp密码" prop="comfirmSftppass" :class="{ 'is-required': !dataForm.seqno }">
                  <el-input v-model="dataForm.comfirmSftppass" type="password" placeholder="确认sftp密码"></el-input>
                </el-form-item>
        </el-col>
    </el-row>

      <el-row>
        <el-col :span="12">
                <el-form-item label="密钥" prop="secret" :class="{ 'is-required': !dataForm.seqno }">
                  <el-input v-model="dataForm.secret" type="password" placeholder="密钥"></el-input>
                </el-form-item>
        </el-col>
        <el-col :span="12">
                <el-form-item label="确认密钥" prop="comfirmSecret" :class="{ 'is-required': !dataForm.seqno }">
                  <el-input v-model="dataForm.comfirmSecret" type="password" placeholder="确认密钥"></el-input>
                </el-form-item>
        </el-col>
      </el-row>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {

      var validateSecret = (rule, value, callback) => {
        if (!this.dataForm.seqno && !/\S/.test(value)) {
          callback(new Error('密钥不能为空'))
        } else {
          callback()
        }
      }

      var validateSftppass = (rule, value, callback) => {
        if (!this.dataForm.seqno && !/\S/.test(value)) {
          callback(new Error('sftp密码不能为空'))
        } else {
          callback()
        }
      }

      var validateLoginpass = (rule, value, callback) => {
        if (!this.dataForm.seqno && !/\S/.test(value)) {
          callback(new Error('登录密码不能为空'))
        } else {
          callback()
        }
      }

      var validateComfirmSecret = (rule, value, callback) => {
        if (!this.dataForm.seqno && !/\S/.test(value)) {
          callback(new Error('确认密钥不能为空'))
        } else if (this.dataForm.secret !== value) {
          callback(new Error('确认密钥与密钥输入不一致'))
        } else {
          callback()
        }
      }

      var validateComfirmSftppass = (rule, value, callback) => {
        if (!this.dataForm.seqno && !/\S/.test(value)) {
          callback(new Error('确认sftp密码不能为空'))
        } else if (this.dataForm.sftppass !== value) {
          callback(new Error('确认sftp密码与sftp密码输入不一致'))
        } else {
          callback()
        }
      }

      var validateComfirmLoginpass = (rule, value, callback) => {
        if (!this.dataForm.seqno && !/\S/.test(value)) {
          callback(new Error('确认登录密码不能为空'))
        } else if (this.dataForm.loginpass !== value) {
          callback(new Error('确认登录密码与登录密码输入不一致'))
        } else {
          callback()
        }
      }

      return {
        visible: false,
        dataForm: {
          seqno: 0,
          orgid: '',
          bankid: '',
          secret: '',
          login: '',
          loginpass: '',
          sftp: '',
          sftppass: '',
          token: '',
          expire: '',
          comfirmSecret: '',
          comfirmLoginpass: '',
          comfirmSftppass: '',
          bankList: [],
          bankList2: []
        },
        dataRule: {

          bankid: [
            { required: true, message: '机构编号不能为空', trigger: 'blur' }
          ],
          orgid: [
            { required: true, message: '机构标识不能为空', trigger: 'blur' }
          ],
          secret: [
            { validator: validateSecret, trigger: 'blur' }
          ],
          comfirmSecret: [
            { validator: validateComfirmSecret, trigger: 'blur' }
          ],
          login: [
            { required: true, message: '登录账号不能为空', trigger: 'blur' }
          ],
          loginpass: [
            { validator: validateLoginpass,  trigger: 'blur' }
          ],
          comfirmLoginpass: [
            { validator: validateComfirmLoginpass, trigger: 'blur' }
          ],
          sftp: [
            { required: true, message: 'sftp账号不能为空', trigger: 'blur' }
          ],
          sftppass: [
            { validator: validateSftppass,  trigger: 'blur' }
          ],
          comfirmSftppass: [
            { validator: validateComfirmSftppass, trigger: 'blur' }
          ]

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.seqno = id || 0
        this.visible = true

        this.$http({
          url: this.$http.adornUrl(`/sys/bdcsecret/item/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.bankList = data.bankList
              this.dataForm.bankList2 = data.bankList2
            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seqno) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcsecret/info/${this.dataForm.seqno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orgid = data.bdcSecret.bdcno
                this.dataForm.bankid = data.bdcSecret.bankid    
                this.dataForm.login = data.bdcSecret.login
                this.dataForm.sftp = data.bdcSecret.sftp
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcsecret/${!this.dataForm.seqno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seqno': this.dataForm.seqno || undefined,
                'bdcno': this.dataForm.orgid,
                'bankid': this.dataForm.bankid,
                'secret': this.dataForm.secret,
                'login': this.dataForm.login,
                'loginpass': this.dataForm.loginpass,
                'sftp': this.dataForm.sftp,
                'sftppass': this.dataForm.sftppass
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
